/*
 * Copyright 2007 Agile Plugins Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * 	http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.googlecode.agileplugins.monkey;

import com.intellij.codeInspection.*;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.psi.search.searches.ReferencesSearch;
import com.intellij.refactoring.rename.RenameProcessor;
import org.jetbrains.annotations.NotNull;

import java.util.Collection;

public class RenameMockFieldInspection extends LocalInspectionTool {

    @NotNull
    public String getGroupDisplayName() {
        return "PurpleMonkeyDishwasher";
    }

    @NotNull
    public String getDisplayName() {
        return "Rename test mock fields";
    }

    @NotNull
    public String getShortName() {
        return "RenameMockField";
    }

    public boolean isEnabledByDefault() {
        return true;
    }

    public ProblemDescriptor[] checkField(@NotNull PsiField field, @NotNull InspectionManager manager, boolean isOnTheFly) {
        if (field.getInitializer() != null) {
            return null;
        }
        if (field.getName() != null && field.getName().startsWith("mock")) {
            return null;
        }
        Collection<PsiReference> references = ReferencesSearch.search(field, GlobalSearchScope.fileScope(field.getContainingFile()), true).findAll();
        for (PsiReference reference : references) {
            if (reference instanceof PsiReferenceExpression) {
                PsiReferenceExpression psiReferenceExpression = (PsiReferenceExpression) reference;
                PsiElement parent = psiReferenceExpression.getParent().getParent();
                if (parent instanceof PsiMethodCallExpression) {
                    PsiMethodCallExpression psiMethodCallExpression = (PsiMethodCallExpression) parent;
                    PsiReferenceExpression methodExpression = psiMethodCallExpression.getMethodExpression();
                    PsiMethod psiMethod = (PsiMethod) methodExpression.resolve();
                    if (psiMethod != null && psiMethod.getName().matches("expect.*Calls?To")) {
                        String newName = getNewName(field);
                        return new ProblemDescriptor[]{manager.createProblemDescriptor(field.getNameIdentifier(), "This field should be called " + newName, new RenameMockFieldQuickFix(newName), ProblemHighlightType.GENERIC_ERROR_OR_WARNING)};
                    }
                }
            }
        }
        return null;
    }

    private String getNewName(PsiField field) {
        return "mock" + ucFirst(field.getName());
    }

    private String ucFirst(String name) {
        return name.substring(0, 1).toUpperCase() + name.substring(1);
    }

    private class RenameMockFieldQuickFix implements LocalQuickFix {
        private String newName;

        public RenameMockFieldQuickFix(String newName) {
            this.newName = newName;
        }

        @NotNull
        public String getName() {
            return "Rename field to " + newName;
        }

        public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor problemdescriptor) {
            PsiField psiField = (PsiField) problemdescriptor.getPsiElement().getParent();
            RenameProcessor renameprocessor = new RenameProcessor(project, psiField, newName,
                    false, false);
            renameprocessor.run();
        }

        @NotNull
        public String getFamilyName() {
            return "PurpleMonkeyDishwasher";
        }
    }
}
